package tech.aistar.controller;

import com.github.pagehelper.PageInfo;
import tech.aistar.model.entity.Product;
import tech.aistar.service.IProductService;
import tech.aistar.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/product/*")
public class ProductController extends HttpServlet {
    private IProductService productService = new ProductServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getPathInfo();

        switch (path){
            case "/index":
                //获取pageNow
                String pageNow = req.getParameter("pageNow");
                if(null == pageNow || pageNow.trim().length() == 0){
                    pageNow = "1";
                }

                PageInfo<Product> pageInfo = productService.findAll(Integer.valueOf(pageNow),4);
                //放入到request作用域中
                req.setAttribute("pageInfo",pageInfo);

                //转发到首页页面
                req.getRequestDispatcher("/WEB-INF/product/index.jsp").forward(req,resp);
                break;
        }
    }
}
